package com.frankie.weibo.miweibo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.frankie.weibo.R;
import com.frankie.weibo.miweibo.bean.StatusBean;
import com.frankie.weibo.miweibo.bean.UserBean;
import com.frankie.weibo.util.image.ImageLoadUtil;

import java.util.List;

/**
 * Created by Frankie on 2016/8/18.
 */

public class StatusItemAdapter extends RecyclerView.Adapter<StatusItemAdapter.StatusViewHolder> {

    private List<StatusBean> mDatas;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public StatusItemAdapter (Context context, List<StatusBean> datas){
        this.mDatas = datas;
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public StatusViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new StatusViewHolder(mLayoutInflater.inflate(R.layout.item_status, null));
    }

    @Override
    public void onBindViewHolder(StatusViewHolder holder, int position) {
        StatusBean statusBean = mDatas.get(position);
        UserBean userBean = statusBean.user;
        ImageLoadUtil.loadCircleImage(userBean.avatar_large, holder.avatarImg);
        holder.screenNameTv.setText(userBean.screen_name);
        holder.createTimeTv.setText(statusBean.created_at);
        holder.contentTv.setText(Html.fromHtml(statusBean.text));
        if(statusBean.retweeted_status != null){
            StatusBean retweetedStatus = statusBean.retweeted_status;
            holder.retweetTextTv.setText(Html.fromHtml(retweetedStatus.text));
            holder.retweetLy.setVisibility(View.VISIBLE);
        }else{
            holder.retweetLy.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class StatusViewHolder extends RecyclerView.ViewHolder {

        private ImageView avatarImg;
        private TextView screenNameTv;
        private TextView createTimeTv;
        private TextView contentTv;
        private LinearLayout retweetLy;

        private TextView retweetTextTv;

        public StatusViewHolder(View itemView) {
            super(itemView);
            avatarImg = (ImageView) itemView.findViewById(R.id.avatar_iv);
            screenNameTv = (TextView) itemView.findViewById(R.id.screen_name_tv);
            createTimeTv = (TextView) itemView.findViewById(R.id.create_time_tv);
            contentTv = (TextView) itemView.findViewById(R.id.status_content_tv);

            retweetLy = (LinearLayout) itemView.findViewById(R.id.retweet_ly);
            retweetTextTv = (TextView) itemView.findViewById(R.id.retweet_text_tv);

        }
    }

}
